class DerivedClassName extends BaseClassName { -----//members of the derived class ------ }
class Account { protected int accno; protected double balance; public void open(int an,int bl) { accno=an; balance=bl; } public void deposit(int amt) { balance=balance+amt; } public void withdraw(int amt) { balance=balance–amt; } public void showBalance() { System.out.println("AccNo is " +accno); System.out.println("Balance is "+balance); } } class SAccount extends Account { public void addInterest(double r,int n) { double si=balance*r*n/100; balance=balance+si; } } class demo { public static void main(String args[]) { Account a=new Account(); a.open(4117,25000); a.deposit(5000); a.withdraw(2000); a.showBalance(); SAccount b=new SAccount(); b.open(3012,10000); b.deposit(2000); b.showBalance(); b.addInterest(10.25,3); b.showBalance(); } }
AccNo is 4117 Balance is 28000 AccNo is 3012 Balance is 12000 AccNo is 3012 Balance is 14750
class Set { protected int n1,n2,n3; public void setData(int x,int y,int z) { n1=x; n2=y; n3=z; } public int sum() { int s=n1+n2+n3; return s; } } class XSet extends Set { public double mean() { double m=sum()/3.0; return m; } } class demo { public static void main(String args[]) { Set a=new Set(); a.setData(4,6,7); System.out.println("Sum of set a is "+a.sum()); XSet b=new XSet(); b.setData(10,20,25); System.out.println("Sum of set b is "+b.sum()); System.out.println("Mean of set b is "+b.mean()); } }
Sum of set a is 17 Sum of set b is 55 Mean of set b is 18.33333
class Rect { protected int L,B; public void setDimension(int m,int n) { L=m; B=n; } public void area() { int a=L*B; System.out.println("Area is "+a); } } class Xrect extends Rect { public void perimeter() { int p=2*(L+B); System.out.println("Perimeter is "+p); } } class demo { public static void main(String args[]) { Rect a=new Rect(); a.setDimension(5,7); a.area(); Xrect b=new Xrect(); b.setDimension(10,20); b.area(); b.perimeter(); } }
Area is 35 Area is 200 Perimeter is 60